package com.example.freshmenapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {
    EditText etName,etPassword,etEmail,etTelephone,etDepartment,etMajor,etRole;
    Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initData();
    }
    private void initData() {
        etName = findViewById(R.id.name);
        etPassword = findViewById(R.id.password);
        etEmail = findViewById(R.id.email);
        etTelephone = findViewById(R.id.telephone);
        etDepartment = findViewById(R.id.department);
        etMajor = findViewById(R.id.major);
        etRole = findViewById(R.id.role);
        btnRegister = findViewById(R.id.register_button);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initView();
            }
        });

    }


    private void initView() {
       final String Tname = etName.getText().toString().trim()+"";
       String Tpassword = etPassword.getText().toString().trim()+"";
       String Temail = etEmail.getText().toString().trim()+"";
       String Ttelephone = etTelephone.getText().toString().trim()+"";
       String Tdepartment = etDepartment.getText().toString().trim()+"";
       String Tmajor = etMajor.getText().toString().trim()+"";
       String Trole = etRole.getText().toString().trim()+"";

       if (TextUtils.isEmpty(Tname) || TextUtils.isEmpty(Tpassword) || TextUtils.isEmpty(Temail) ||
               TextUtils.isEmpty(Ttelephone)||TextUtils.isEmpty(Tdepartment) || TextUtils.isEmpty(Tmajor) ||
               TextUtils.isEmpty(Trole))  {
           Toast.makeText(RegisterActivity.this, "请填写完数据", Toast.LENGTH_SHORT).show();
           return;
       }



        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name",Tname);
            jsonObject.put("password",Tpassword);
            jsonObject.put("email",Temail);
            jsonObject.put("telephone",Ttelephone);
            jsonObject.put("department",Tdepartment);
            jsonObject.put("major",Tmajor);
            jsonObject.put("role",Trole);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);
        String url = "http://192.168.0.105:8080/freshmenapp/user/register";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {

                if (jsonObject.optBoolean("success",true)) {
                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);

                    intent.setClass(RegisterActivity.this, LoginActivity.class);
                    intent.putExtra("name",Tname);
                    startActivity(intent);

                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(RegisterActivity.this, "错误", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(RegisterActivity.this,"请求失败",Toast.LENGTH_LONG).show();
            }
        });
        //讲请求内容嵌入队列中
        requestQueue.add(jsonObjectRequest);


    }




}
